package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.base.RecycleViewDivider;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.bean.YanShouDaiDetailsBean;
import com.wnhz.work.yz.bean.YanShouJiChaBean;
import com.wnhz.work.yz.utils.LogUtil;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class YanShouan442Activity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;
    private List<YanShouDaiDetailsBean.InfoEntity> infoEntities = new ArrayList<>();

    private BaseRVAdapter adapter;

    private TextView rightText, submit;
    private List<String> list = new ArrayList<>();

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Finish1 event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);//反注册EventBus
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yan_shou_dai_chan_pin2);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        rightText = (TextView) findViewById(R.id.view_main_right_text);

        rightText.setText("编辑");
        rightText.setTextSize(12);
        rightText.setVisibility(View.VISIBLE);
        rightText.setOnClickListener(this);

        EventBus.getDefault().register(this);
        title.setText("报检套餐列表");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        submit = (TextView) findViewById(R.id.submit);
        submit.setText("提交");
        submit.setTextSize(12);
        submit.setVisibility(View.GONE);
        submit.setOnClickListener(this);

        getDetails(getIntent().getStringExtra("orderid"));
    }

    private void initView() {
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
        recycler.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, 1, getResources().getColor(R.color.line_color)));

        adapter = new BaseRVAdapter(this, infoEntities) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_dai_chanpin2;
            }

            @Override
            public void onBind(final BaseViewHolder holder, final int position) {
                //   x.image().bind(holder.getImageView(R.id.img), info.get(position).getImg());
                ImageLoader.getInstance().displayImage(infoEntities.get(position).getImg(), holder.getImageView(R.id.img));

                holder.setTextView(R.id.bianhao, "编号：" + infoEntities.get(position).getNumber());
                holder.setTextView(R.id.name, infoEntities.get(position).getName());
                holder.setTextView(R.id.house, infoEntities.get(position).getHouse());
                holder.setTextView(R.id.sku, infoEntities.get(position).getInfo());

                if (infoEntities.get(position).isVisiabled()) {
                    holder.getView(R.id.ll_select).setVisibility(View.VISIBLE);
                }

                if (infoEntities.get(position).getSelf_check().equals("1")) {
                    holder.getView(R.id.ll_select).setVisibility(View.VISIBLE);
                    holder.setCheckStats(R.id.btn_allCheck, true);
                }

                holder.getView(R.id.btn_allCheck).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final String nowBeanChecked = infoEntities.get(position).getSelf_check();

                        if (view instanceof CheckBox) {
                            if (nowBeanChecked.equals("1")) {
                                holder.setCheckStats(R.id.btn_allCheck, true);
                                MyToast("该套餐已成功报检");
                            } else {
                                CheckBox checkBox = (CheckBox) view;
                                infoEntities.get(position).setChecked(checkBox.isChecked());

                                Log.e("=== 子item是否勾选 ===", " ---- " + checkBox.isChecked());
                                notifyDataSetChanged();
                            }
                        }
                    }
                });
//
//                holder.getImageView(R.id.select).setImageResource(R.mipmap.ic_choose_li);
//            } else {
//                holder.getImageView(R.id.select).setImageResource(R.mipmap.ic_choose_nor);

//                if (type.equals("0")) {
//                    holder.getTextView(R.id.status).setVisibility(View.GONE);
//                } else if (type.equals("0")) {
//                    if ("0".equals(info.get(position).getStatus())) {
//                        holder.setTextView(R.id.status, "未检");
//                        holder.getTextView(R.id.status).setTextColor(getResources().getColor(R.color.text102));
//                    } else if ("1".equals(info.get(position).getStatus())) {
//                        holder.setTextView(R.id.status, "不合格");
//                        holder.getTextView(R.id.status).setTextColor(getResources().getColor(R.color.red));
//                    } else if ("2".equals(info.get(position).getStatus())) {
//                        holder.setTextView(R.id.status, "已通过");
//                        holder.getTextView(R.id.status).setTextColor(getResources().getColor(R.color.main_color));
//                    }
//                }

                holder.getView(R.id.ll_max).

                        setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

//                        getDetails(getIntent().getStringExtra("orderid"));
                            }
                        });
            }
        }

        ;
        recycler.setAdapter(adapter);
    }


    private void getDetails(String orderid) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", orderid);
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(Url.DECORATE_WAITINGCHECKDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("----套餐列表详情", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");

                    super.onSuccess(result);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YanShouDaiDetailsBean bean = gson.fromJson(result, YanShouDaiDetailsBean.class);

                        infoEntities = bean.getInfo();

                        initView();
//                        //待安装 -- 套餐列表
//                        startActivity(new Intent(YanShou2AnActivity.this, YanShouan44Activity.class).putExtra("bean", bean).putExtra("orderid", orderid).putExtra("type", type + ""));
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.btn_main_right:
                if (!infoEntities.isEmpty()) {
                    rightText.setVisibility(View.GONE);
                    submit.setVisibility(View.VISIBLE);

                    for (int i = 0; i < infoEntities.size(); i++) {
                        YanShouDaiDetailsBean.InfoEntity infoEntity = (YanShouDaiDetailsBean.InfoEntity) infoEntities.get(i);
                        infoEntity.setVisiabled(true);
                    }
                }
                adapter.notifyDataSetChanged();

                break;

            case R.id.submit:
                for (int i = 0; i < infoEntities.size(); i++) {
                    YanShouDaiDetailsBean.InfoEntity infoEntity = (YanShouDaiDetailsBean.InfoEntity) infoEntities.get(i);
                    if (infoEntity.getSelf_check().equals("0") && infoEntity.isChecked()) {
                        list.add(infoEntity.getPid() + "-");
                    }
                }

                if (list.get(0).equals("-")) {
                    MyToast("请选择报检套餐");
                    return;
                }

                String aaa = list.toString();

                upload("-" + aaa);
                break;
        }
    }

    private void upload(String pid) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", getIntent().getStringExtra("orderid"));
        map.put("pid", pid);

        showDialog();
        XUtil.Post(Url.ANZHUANG_BAOJIANTIJIAO, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("--竣工报检--", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        finish();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });
    }
}
